<?php
/**
 * Description: 阅读打卡笔记列表
 * User: lizhen
 * Date: 14/10/16
 * Time: 16:03
 */

class GetNoteYuedudaka implements iController {


    public function output($queryArgs) {
        global $uddb;

        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        $userId = $queryArgs['user_id'];
        $offset = !empty($queryArgs['offset']) ? $queryArgs['offset'] : 0;
        $limit  = !empty($queryArgs['limit']) ? $queryArgs['limit'] : 20;

        if ($limit > 20) $limit = 20;

        $sql = "select * from history_user_primary_question where user_id=:uid and is_note = 1 order by id desc  limit {$offset} , {$limit} ";
        $result = $uddb->getResults($sql, [':uid' => $userId]);

        $data = [];

        if (!empty($result)) {

            foreach ($result as $k => $v) {

                $exerciseInfo = $this->_getExerciseInfo($v->exercise_id);

                $data[] = [
                    'user_id'       => (int)$v->user_id,
                    'exercise_id'   => (int)$v->exercise_id,
                    'question_id'   => (int)$v->question_id,
                    'is_note'       => (int)$v->is_note,
                    'user_answer'   => nl2br((string)$v->user_answer),
                    'acting_time'   => date('Y-m-d',strtotime($v->acting_time)),
                    'title'         => $exerciseInfo->name,
                    'type_id'       => (int)$exerciseInfo->type_id
                ];
            }

        }
        return genOutput(1, ['list' => $data]);
    }

    /**
     * 获取作业信息
     *
     * @param $exerciseId
     * @return array|null
     */
    private function _getExerciseInfo($exerciseId) {
        global $cmsdb;

        $result = $cmsdb->getRow("select * from data_primary_exercise where id=:eid",
            [':eid'=>$exerciseId]);
        return $result;
    }

}